// pages/licenseInfo/captcha/captcha.js
const app = getApp()
import { requestApi } from '../../../api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
    inputCode: '',
    seller_uid: '',
    submitButton: false,
    type: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      seller_uid: options.seller_uid,
      type: options.type
    })

    // 请求验证码
    this.getCertificatesVcode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputCode: function (e) {
    if (e.detail.value != '') {
      this.setData({
        submitButton: true
      })
    } else {
      this.setData({
        submitButton: false
      })
    }
  },

  submit: function (e) {
    let code = e.detail.value.code.trim()

    if (code == '') return false

    if (code == this.data.code) {
      wx.redirectTo({
        url: '../licenseInfo/licenseInfo?type='+ this.data.type +'&seller_uid=' + this.data.seller_uid + '&code=' + this.data.code,
      })
    } else {
      wx.showModal({
        showCancel: false,
        content: '验证码错误，请重试！',
      })
    }
  },

  getCertificatesVcode: function () {
    let diu = wx.getStorageSync('diu')
    let type = this.data.type

    app.request({
      url: requestApi.getCertificatesVcodetUrl,
      login: false,
      data: {
        diu: diu,
        type: type,
      },
    }, (data) => { 
      this.setData({
        code: data.original.data.vcode
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    })
  },
})